package com.android.love.web;

import android.os.Environment;

import java.io.File;

/**
 * I/O 读写通用工具类
 */
public class IOUtils {

	private static final String APPLICATION_FOLDER = "culiu";
	private static final String DOWNLOAD_FOLDER = "downloads";

	/**
	 * 获取一个应用目录，创建它如果不存在
	 * 
	 * @return The application folder.
	 */
	public static File getApplicationFolder() {
		File root = Environment.getExternalStorageDirectory();
		if (root.canWrite()) {

			File folder = new File(root, APPLICATION_FOLDER);

			if (!folder.exists()) {
				folder.mkdir();
			}

			return folder;

		} else {
			return null;
		}
	}

	/**
	 * 获取一个应用下载目录，创建它如果不存在
	 * 
	 * @return
	 */
	public static File getDownloadFolder() {
		File root = getApplicationFolder();

		if (root != null) {

			File folder = new File(root, DOWNLOAD_FOLDER);

			if (!folder.exists()) {
				folder.mkdir();
			}

			return folder;

		} else {
			return null;
		}
	}

}
